package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.db.domain.MarketAdExample;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.service.AdminAdService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminAdServiceImpl implements AdminAdService {
    @Override
    public List<MarketAd> list(String page, String limit, String name, String content, String sort, String order) {
        SqlSession session = MybatisUtil.getSession();
        MarketAdMapper mapper = session.getMapper(MarketAdMapper.class);
        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        criteria.andDeletedEqualTo(false);
        marketAdExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketAd> adList = mapper.selectByExample(marketAdExample);
        session.commit();
        session.close();
        return adList;
    }

    @Override
    public MarketAd insert(MarketAd marketAd) {
        SqlSession session = MybatisUtil.getSession();
        MarketAdMapper mapper = session.getMapper(MarketAdMapper.class);
        mapper.insert(marketAd);
        session.commit();
        session.close();
        return marketAd;
    }

    @Override
    public MarketAd update(MarketAd marketAd) {
        SqlSession session = MybatisUtil.getSession();
        MarketAdMapper mapper = session.getMapper(MarketAdMapper.class);
        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        criteria.andIdEqualTo(marketAd.getId());
        mapper.updateByExampleSelective(marketAd, marketAdExample);
        //todo:更新完后是否要再查询一次
        marketAd = mapper.selectOneByExampleSelective(marketAdExample);
        session.commit();
        session.close();
        return marketAd;
    }

    @Override
    public boolean delete(Integer id) {
        SqlSession session = MybatisUtil.getSession();
        MarketAdMapper mapper = session.getMapper(MarketAdMapper.class);
        int delete = mapper.deleteByPrimaryKey(id);
        session.commit();
        session.close();
        if (delete == 1) {
            return true;
        }
        return false;

    }

    @Override
    public boolean deleteLogically(Integer id) {
        SqlSession session = MybatisUtil.getSession();
        MarketAdMapper mapper = session.getMapper(MarketAdMapper.class);
        int delete = mapper.logicalDeleteByPrimaryKey(id);
        session.commit();
        session.close();
        if (delete == 1) {
            return true;
        }
        return false;

    }


}
